package com.tlgen.orm.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class PageResult<T> {

    private Integer pageNum;
    private Integer pageSize;
    private Integer total;
    private List<T> records;
    private Integer totalPages;
    private String nextCursor; // Base64编码的游标值
    private Boolean hasNext;   // 是否有下一页

    /**
     * 计算并设置总页数（应在设置total或pageSize后调用）
     */
    public void calculateTotalPages() {
        if (pageSize == null || pageSize == 0 || total == null) {
            totalPages = 0; // 防止除零错误
        } else {
            totalPages = (int) Math.ceil((double) total / pageSize);
        }
    }

    // 重写setTotal和setPageSize方法确保计算逻辑
    public void setTotal(Integer total) {
        this.total = total;
        calculateTotalPages();
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        calculateTotalPages();
    }

}
